package cn.wolfcode.edu.web.controller;

import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.query.AttendanceQueryObject;
import cn.wolfcode.edu.service.IAttendanceService;
import cn.wolfcode.edu.service.IEmployeeService;
import cn.wolfcode.edu.util.JsonResult;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("attendance")
public class AttendanceController {
    @Autowired
    private IAttendanceService attendanceService;
    @Autowired
    private IEmployeeService employeeService;
    //导航页面
    @RequestMapping("")
    public String index() {
        return "attendance";
    }
    //查询数据
    @ResponseBody
    @RequestMapping("list")
    public PageResult list(AttendanceQueryObject qo) {
        return attendanceService.list(qo);
    }
    //签到
    @ResponseBody
    @RequestMapping("signIn")
    public JsonResult signIn() {
        Subject subject = SecurityUtils.getSubject();
        Employee user = (Employee) subject.getPrincipal();
        Employee employee = employeeService.selectByUsername(user.getUsername());
        user.setId(employee.getId());
        JsonResult jsonResult = new JsonResult();
        try {
            attendanceService.signIn(user);
        } catch (Exception e) {
            jsonResult = jsonResult.remark(e.getMessage());
        }
        return jsonResult;
    }
    //签退
    @ResponseBody
    @RequestMapping("signBack")
    public JsonResult signBack() {
        Subject subject = SecurityUtils.getSubject();
        Employee user = (Employee) subject.getPrincipal();
        Employee employee = employeeService.selectByUsername(user.getUsername());
        user.setId(employee.getId());
        JsonResult jsonResult = new JsonResult();
        try {
            attendanceService.signBack(user);
        } catch (Exception e) {
            jsonResult = jsonResult.remark(e.getMessage());
        }
        return jsonResult;
    }
}
